package com.shujia.dao;

import com.shujia.bean.Student;
import com.shujia.util.JDBCUtil;

import java.sql.*;

public class StudentDaoImpl implements StudentDao {


    @Override
    public Student queryStudentById(String id) {


        Student student = null;

        try {

            //获取jdbc连接
            Connection con = JDBCUtil.getConnection();

            PreparedStatement stat = con.prepareStatement("select * from student where id=?");

            stat.setString(1, id);

            //执行查询
            ResultSet resultSet = stat.executeQuery();

            if (resultSet.next()) {
                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String clazz = resultSet.getString("clazz");

                student = new Student(id, name, age, gender, clazz);
            }

            //光比连接,  将连接放回到连接池
            con.close();


        } catch (Exception e) {
            e.printStackTrace();
        }


        return student;

    }
}
